package com.my.rabbitmq.six;

import com.my.utils.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2023/11/30 12:24
 */
public class ReceiveLogsTopic02 {

    // 交换机名称
    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception{

        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName, false, false,false, null);
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");

        System.out.println("等待接收消息");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("队列" + queueName + "的routingKey:" + message.getEnvelope().getRoutingKey());
            System.out.println("接收到消息:" + new String(message.getBody()));
        };
        
        // 接收消息
        channel.basicConsume(queueName, true, deliverCallback, (consumerTag) -> {
            System.out.println(consumerTag + "接收消息失败");
        });

    }


}
